home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / sound / fftscop4.zip / FFTSCOPE.C < prev    next >
C/C++ Source or Header  |  1994-05-30  |  30KB  |  1,392 lines

  1. #include <dos.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <alloc.h>
  6. #include <string.h>
  7. #include <mem.h>
  8. #include <math.h>
  9. #define    TWO_PI    ((double)2.0 * M_PI)
  10.  
  11. typedef unsigned char DacPalette256[256][3];
  12. unsigned char far *VGAmem;
  13. struct text_info tiGFX;
  14. struct REGPACK regp;
  15. union  REGS reg;
  16. int Linecolor=1,WritePage=0,HighSpeed=0,Triggerbuffer=999;
  17. int minX=0,minY=0,maxx=320,maxy=200;
  18.  
  19. DacPalette256 Hallo;
  20.  
  21. /* Globals */
  22.  
  23. int    samples, power;
  24. double    real[2048], imag[2048], max, Pegel=10,FFTmax;
  25. FILE    *fpi, *fpo;
  26.  
  27. /* Prototypes and forward declarations */
  28.  
  29. void    fft(void), max_amp(void);
  30. int    permute(int);
  31. double    magnitude(int);
  32. void dspwrite ( unsigned char );
  33. unsigned char dspread ( void );
  34. unsigned Buffer[2048];
  35. unsigned SBuffer[8900];
  36. unsigned char far *data;
  37. unsigned char far *aligned;
  38. unsigned char far aligned_physical;
  39.  
  40. /* Getvgapalette256 gets the entire 256 color palette */
  41. /* PalBuf contains RGB values for all 256 colors      */
  42. /* R,G,B values range from 0 to 63                  */
  43. /* Usage:                          */
  44. /*  DacPalette256 dac256;                  */
  45. /*                              */
  46. /* getvgapalette256(&dac256);                  */
  47. void getvgapalette256(DacPalette256 *PalBuf)
  48. {
  49.   regp.r_ax = 0x1017;
  50.   regp.r_bx = 0;
  51.   regp.r_cx = 256;
  52.   regp.r_es = FP_SEG(PalBuf);
  53.   regp.r_dx = FP_OFF(PalBuf);
  54.   intr(0x10,®p);
  55. }
  56.  
  57. /* Setvgapalette256 sets the entire 256 color palette */
  58. /* PalBuf contains RGB values for all 256 colors      */
  59. /* R,G,B values range from 0 to 63                  */
  60. /* Usage:                          */
  61. /*  DacPalette256 dac256;                  */
  62. /*                              */
  63. /* setvgapalette256(&dac256);                  */
  64. void setvgapalette256(DacPalette256 *PalBuf)
  65. {
  66.   regp.r_ax = 0x1012;
  67.   regp.r_bx = 0;
  68.   regp.r_cx = 256;
  69.   regp.r_es = FP_SEG(PalBuf);
  70.   regp.r_dx = FP_OFF(PalBuf);
  71.   intr(0x10,®p);
  72. }
  73.  
  74.  
  75.  
  76. void Grafik(int GFXmode)  // 13,      14,     17mono,   18,      19vga,  -1,3
  77. {                         //320x200, 640x200, 640x480, 640x480, 320x200, text
  78. VGAmem=(char far *)MK_FP(0xa000,0);
  79. if (GFXmode>=0)
  80.   {
  81.    gettextinfo(&tiGFX);
  82.    regp.r_ax = GFXmode;
  83.    intr(0x10,®p);
  84.   }
  85. if (GFXmode<0)
  86.   {
  87.    regp.r_ax = tiGFX.currmode;
  88.    intr(0x10,®p);
  89.   }
  90. }
  91.  
  92.  
  93.  
  94. void Putpixel(int x, int y)
  95. {
  96.  reg.h.ah = 0x0c;
  97.  reg.h.al = Linecolor;
  98.  reg.h.bh = WritePage;
  99.  reg.x.cx = x;
  100.  reg.x.dx = y;
  101.  int86(0x10,®,®);
  102. }
  103.  
  104.  
  105.  
  106. void Putpixel2(int x, int y)
  107. {
  108.  reg.h.ah = 0x0c;
  109.  reg.h.al = 0;
  110.  reg.h.bh = WritePage;
  111.  reg.x.cx = x;
  112.  reg.x.dx = y;
  113.  int86(0x10,®,®);
  114. }
  115.  
  116.  
  117.  
  118. void Putpixel3(int x, int y)
  119. {
  120.  reg.h.ah = 0x0c;
  121.  reg.h.al = 235-y;
  122.  reg.h.bh = WritePage;
  123.  reg.x.cx = x;
  124.  reg.x.dx = y;
  125.  int86(0x10,®,®);
  126. }
  127.  
  128.  
  129.  
  130. int Getpixel(int x, int y)
  131. {
  132.  if(x<minX)return 0;
  133.  if(y<minY)return 0;
  134.  if(x>=maxx)return 0;
  135.  if(y>=maxy)return 0;
  136.  reg.h.ah = 0x0d;
  137.  reg.h.bh = WritePage;
  138.  reg.x.cx = x;
  139.  reg.x.dx = y;
  140.  int86(0x10,®,®);
  141.  return reg.h.al;
  142. }
  143.  
  144.  
  145.  
  146. void singlecolor256(int nr,int r,int g,int b)
  147. {
  148.  reg.h.ah = 0x10;
  149.  reg.h.al = 0x10;
  150.  reg.x.bx = nr;
  151.  reg.h.ch = g;
  152.  reg.h.cl = b;
  153.  reg.h.dh = r;
  154.  int86(0x10,®,®);
  155. }
  156.  
  157.  
  158.  
  159. void singlecolor16(int nr,int r,int g,int b)
  160. {
  161.  reg.h.ah=0x10;
  162.  reg.h.al=0x07;
  163.  reg.h.bl=nr;
  164.  int86(0x10,®,®);
  165.  nr=reg.h.bh;
  166.  reg.h.ah = 0x10;
  167.  reg.h.al = 0x10;
  168.  reg.x.bx = nr;
  169.  reg.h.ch = g;
  170.  reg.h.cl = b;
  171.  reg.h.dh = r;
  172.  int86(0x10,®,®);
  173. }
  174.  
  175.  
  176.  
  177. void linie(double x1,double y1,double x2,double y2)
  178. {
  179.  double a,b,dx,dy,e;
  180.  dx=x1-x2;
  181.  dy=y1-y2;
  182.  if(dx==0)
  183.       {
  184.        if(dy>=0) for(e=y2;e<=y1;e++)Putpixel(x1,e);
  185.        else      for(e=y1;e<=y2;e++)Putpixel(x1,e);
  186.        return;
  187.       }
  188.  if(dy==0)
  189.       {
  190.        if(dx>=0) for(e=x2;e<=x1;e++)Putpixel(e,y1);
  191.        else      for(e=x1;e<=x2;e++)Putpixel(e,y1);
  192.        return;
  193.       }
  194.  e=y1-(dy*x1)/dx;
  195. if(abs(dx)>=abs(dy))
  196.    {
  197.     if(dx>=0)
  198.     {
  199.      for(a=x2;a<=x1;a++){ b=e+(a*dy)/dx; Putpixel(a,b); }
  200.     }
  201.     else
  202.     {
  203.      for(a=x1;a<=x2;a++){ b=e+(a*dy)/dx; Putpixel(a,b); }
  204.     }
  205.    }
  206. if(abs(dx)<abs(dy))
  207.    {
  208.     if(dy>=0)
  209.     {
  210.      for(a=y2;a<=y1;a++){ b=((a-e)*dx)/dy; Putpixel(b,a); }
  211.     }
  212.     else
  213.     {
  214.      for(a=y1;a<=y2;a++){ b=((a-e)*dx)/dy; Putpixel(b,a); }
  215.     }
  216.    }
  217. }
  218.  
  219.  
  220.  
  221.  
  222.  
  223. void MakePalette()
  224. {
  225.  int frag;
  226.  for(frag=0;frag<256;frag++)
  227.    {
  228.     if(frag<32)
  229.            {
  230.         Hallo[frag][0]=0;
  231.         Hallo[frag][1]=0;
  232.         Hallo[frag][2]=2*frag;
  233.            }
  234.     if((frag>=32)&&(frag<96))
  235.                  {
  236.                   Hallo[frag][0]=0;
  237.                   Hallo[frag][1]=(frag-32);
  238.                   Hallo[frag][2]=(95-frag);
  239.                  }
  240.     if((frag>=96)&&(frag<128))
  241.                   {
  242.                    Hallo[frag][0]=2*(frag-96);
  243.                    Hallo[frag][1]=63;
  244.                    Hallo[frag][2]=0;
  245.                   }
  246.     if((frag>=128)&&(frag<192))
  247.                    {
  248.                 Hallo[frag][0]=63;
  249.                 Hallo[frag][1]=(191-frag);
  250.                 Hallo[frag][2]=(frag-128);
  251.                    }
  252.     if(frag>=192)
  253.                 {
  254.               Hallo[frag][0]=63;
  255.           Hallo[frag][1]=0;
  256.           Hallo[frag][2]=0;
  257.           if(frag<223)Hallo[frag][2]=2*(222-frag);
  258.              }
  259.    }
  260.  setvgapalette256(&Hallo);
  261. }
  262.  
  263.  
  264. //------------------------------------------------------------------------------
  265. void dspwrite ( unsigned char c )
  266. {
  267.     while(inportb(0x022C)&0x80);
  268.     outportb(0x022C,c);
  269. }
  270. //------------------------------------------------------------------------------
  271. void sbinit ( void )
  272. {
  273.     unsigned short x;
  274.     inportb(0x022E);
  275.     outportb(0x0226,0x01);
  276.     inportb(0x0226);
  277.     inportb(0x0226);
  278.     inportb(0x0226);
  279.     inportb(0x0226);
  280.     outportb(0x0226,0x00);
  281.     for(x=0;x<100;x++)
  282.     {
  283.         if(inportb(0x022E)&0x80)
  284.         {
  285.             if(inportb(0x022A)==0xAA) break;
  286.         }
  287.     }
  288.     if(x==100)
  289.     {
  290.         printf("Sound Blaster not found at 0220h\n");
  291.         exit(1);
  292.     }
  293. }
  294. //------------------------------------------------------------------------------
  295. void sbmalloc ( void )
  296. {
  297.     unsigned long physical;
  298.     data=farmalloc(80000L);
  299.     if(data==NULL)
  300.     {
  301.         printf("Memory Allocation Error\n");
  302.         exit(1);
  303.     }
  304.     physical=((unsigned long)FP_OFF(data))+(((unsigned long)FP_SEG(data))<<4);
  305.     physical+=0x0FFFFL;
  306.     physical&=0xF0000L;
  307.     aligned_physical=(physical>>16)&15;
  308.     aligned=MK_FP(((unsigned short)aligned_physical<<12)&0xF000,0);
  309. }
  310. //------------------------------------------------------------------------------
  311. void sbsettc ( unsigned char tc )
  312. // tc = time constant = 256L - (1000000UL/samples per second)
  313. {
  314.     inportb(0x022E);
  315.     dspwrite(0x40);
  316.     dspwrite(tc);
  317. }
  318. //------------------------------------------------------------------------------
  319. void sbrec ( unsigned short len )
  320. // len = number of bytes to record to unsigned char *aligned (<=65000)
  321. {
  322.     len--;
  323.     outportb(0x0A,0x05);
  324.     outportb(0x0C,0x00);
  325.     outportb(0x0B,0x45);
  326.     outportb(0x02,0);
  327.     outportb(0x02,0);
  328.     outportb(0x83,aligned_physical);
  329.     outportb(0x03,(unsigned char)(len&0xFF));
  330.     outportb(0x03,(unsigned char)((len>>8)&0xFF));
  331.     outportb(0x0A,0x01);
  332.     dspwrite(0x24);
  333.     dspwrite((unsigned char)(len&0xFF));
  334.     dspwrite((unsigned char)((len>>8)&0xFF));
  335. }
  336. //------------------------------------------------------------------------------
  337. void sbrec2 ( unsigned short len )
  338. // len = number of bytes to record to unsigned char *aligned (<=65000)
  339. {
  340.     len--;
  341.     outportb(0x0A,0x05);
  342.     outportb(0x0C,0x00);
  343.     outportb(0x0B,0x45);
  344.     outportb(0x02,0);
  345.     outportb(0x02,0);
  346.     outportb(0x83,aligned_physical);
  347.     outportb(0x03,(unsigned char)(len&0xFF));
  348.     outportb(0x03,(unsigned char)((len>>8)&0xFF));
  349.     outportb(0x0A,0x01);
  350.     dspwrite(0x48);
  351.     dspwrite((unsigned char)(len&0xFF));
  352.     dspwrite((unsigned char)((len>>8)&0xFF));
  353.     dspwrite(0x99);
  354. }
  355. //------------------------------------------------------------------------------
  356. unsigned short dmacount ( void )
  357. {
  358.     unsigned short x;
  359.     x=inportb(0x03);
  360.     x|=inportb(0x03)<<8;
  361.     if(x==0xFFFF) inportb(0x022E);
  362.     return(x);
  363. }
  364. //------------------------------------------------------------------------------
  365. void recording(int e,int f)
  366. {
  367.  int m,n=Triggerbuffer -e -30,o;